import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

public class NameValidator extends JFrame {

    private JTextField nameField;
    private JRadioButton chineseRadio;
    private JRadioButton englishRadio;
    private JRadioButton universalRadio;
    private JButton validateButton;
    private JTextArea resultArea;

    public NameValidator() {
        // 设置窗口
        setTitle("姓名正则校验工具");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        
        // 创建顶部面板
        JPanel topPanel = new JPanel(new BorderLayout(10, 10));
        topPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        
        // 输入区域
        JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
        inputPanel.add(new JLabel("请输入姓名:"), BorderLayout.WEST);
        nameField = new JTextField();
        inputPanel.add(nameField, BorderLayout.CENTER);
        
        // 单选按钮组
        JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 5));
        ButtonGroup group = new ButtonGroup();
        chineseRadio = new JRadioButton("中文姓名", true);
        englishRadio = new JRadioButton("英文姓名");
        universalRadio = new JRadioButton("通用模式");
        group.add(chineseRadio);
        group.add(englishRadio);
        group.add(universalRadio);
        radioPanel.add(chineseRadio);
        radioPanel.add(englishRadio);
        radioPanel.add(universalRadio);
        
        // 验证按钮
        validateButton = new JButton("验证姓名");
        validateButton.setBackground(new Color(70, 130, 180));
        validateButton.setForeground(Color.WHITE);
        validateButton.setFocusPainted(false);
        
        // 结果区域
        resultArea = new JTextArea();
        resultArea.setEditable(false);
        resultArea.setFont(new Font("等线", Font.PLAIN, 16));
        resultArea.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
        JScrollPane scrollPane = new JScrollPane(resultArea);
        
        // 添加组件到顶部面板
        topPanel.add(inputPanel, BorderLayout.NORTH);
        topPanel.add(radioPanel, BorderLayout.CENTER);
        topPanel.add(validateButton, BorderLayout.SOUTH);
        
        // 添加到主窗口
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        
        // 添加事件监听
        validateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                validateName();
            }
        });
        
        // 设置窗口可见
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    private void validateName() {
        String name = nameField.getText().trim();
        if (name.isEmpty()) {
            resultArea.setText("错误：请输入姓名");
            return;
        }
        
        boolean isValid = false;
        String regexType = "";
        String regexPattern = "";
        
        if (chineseRadio.isSelected()) {
            regexType = "中文姓名";
            regexPattern = "^[\u4e00-\u9fa5]{2,10}(·[\u4e00-\u9fa5]{2,10})?$";
            isValid = Pattern.matches(regexPattern, name);
        } 
        else if (englishRadio.isSelected()) {
            regexType = "英文姓名";
            regexPattern = "^[A-Z][a-z]{1,19}(?:[\\s-][A-Z][a-z]{1,19})*$";
            isValid = Pattern.matches(regexPattern, name);
        } 
        else {
            regexType = "通用模式";
            regexPattern = "^([\u4e00-\u9fa5]{2,10}|[A-Z][a-z]{1,19})(?:[·\\s-][\u4e00-\u9fa5A-Za-z]{2,20})*$";
            isValid = Pattern.matches(regexPattern, name);
        }
        
        String result = "验证类型: " + regexType + "\n" +
                        "正则表达式: " + regexPattern + "\n" +
                        "输入姓名: \"" + name + "\"\n\n" +
                        "验证结果: " + (isValid ? "✓ 合法姓名" : "✗ 不合法姓名");
        
        resultArea.setText(result);
    }
    
    // 静态验证方法（可在其他代码中使用）
    public static boolean isValidChineseName(String name) {
        String regex = "^[\u4e00-\u9fa5]{2,10}(·[\u4e00-\u9fa5]{2,10})?$";
        return Pattern.matches(regex, name);
    }
    
    public static boolean isValidEnglishName(String name) {
        String regex = "^[A-Z][a-z]{1,19}(?:[\\s-][A-Z][a-z]{1,19})*$";
        return Pattern.matches(regex, name);
    }
    
    public static boolean isValidUniversalName(String name) {
        String regex = "^([\u4e00-\u9fa5]{2,10}|[A-Z][a-z]{1,19})(?:[·\\s-][\u4e00-\u9fa5A-Za-z]{2,20})*$";
        return Pattern.matches(regex, name);
    }

    public static void main(String[] args) {
        // 使用示例
        System.out.println("中文姓名测试:");
        System.out.println("张三: " + isValidChineseName("张三")); // true
        System.out.println("诸葛亮: " + isValidChineseName("诸葛亮")); // true
        System.out.println("司马·买买提: " + isValidChineseName("司马·买买提")); // true
        System.out.println("A张三: " + isValidChineseName("A张三")); // false
        
        System.out.println("\n英文姓名测试:");
        System.out.println("John Smith: " + isValidEnglishName("John Smith")); // true
        System.out.println("Mary-Jane Watson: " + isValidEnglishName("Mary-Jane Watson")); // true
        System.out.println("alan turing: " + isValidEnglishName("alan turing")); // false (首字母未大写)
        System.out.println("James: " + isValidEnglishName("James")); // true
        
        System.out.println("\n通用模式测试:");
        System.out.println("张三: " + isValidUniversalName("张三")); // true
        System.out.println("John Smith: " + isValidUniversalName("John Smith")); // true
        System.out.println("张·John: " + isValidUniversalName("张·John")); // true
        System.out.println("123: " + isValidUniversalName("123")); // false
        
        // 启动GUI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NameValidator();
            }
        });
    }
}